<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MPU6050寄存器数据读取演示</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        accent: '#F59E0B',
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .register-grid {
                display: grid;
                grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
                gap: 0.75rem;
            }
            .register-active {
                animation: pulse 1.5s infinite;
            }
            .data-flow {
                animation: flow 1.5s ease-out forwards;
            }
            @keyframes pulse {
                0%, 100% {
                    box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7);
                }
                70% {
                    box-shadow: 0 0 0 10px rgba(59, 130, 246, 0);
                }
            }
            @keyframes flow {
                0% {
                    transform: translateX(0);
                    opacity: 0;
                }
                50% {
                    opacity: 1;
                }
                100% {
                    transform: translateX(100px);
                    opacity: 0;
                }
            }
            .pin {
                position: relative;
                height: 20px;
                width: 2px;
                background-color: #64748B;
            }
            .pin:after {
                content: '';
                position: absolute;
                top: -3px;
                left: -3px;
                width: 8px;
                height: 8px;
                border-radius: 50%;
                background-color: #64748B;
            }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen font-sans">
    <!-- 顶部导航 -->
    <header class="bg-primary text-white shadow-md">
        <div class="container mx-auto px-4 py-4 flex justify-between items-center">
            <h1 class="text-2xl font-bold">MPU6050 寄存器数据读取演示</h1>
            <div class="flex space-x-4">
                <button id="resetBtn" class="bg-white/20 hover:bg-white/30 px-3 py-1 rounded transition">
                    <i class="fa fa-refresh mr-1"></i>重置
                </button>
                <button id="infoBtn" class="bg-white/20 hover:bg-white/30 px-3 py-1 rounded transition">
                    <i class="fa fa-info-circle mr-1"></i>帮助
                </button>
            </div>
        </div>
    </header>

    <!-- 主内容区 -->
    <main class="container mx-auto px-4 py-8">
        <!-- 说明部分 -->
        <section class="bg-white rounded-lg shadow-md p-6 mb-8">
            <h2 class="text-xl font-semibold mb-4 text-dark">MPU6050 数据读取原理</h2>
            <p class="mb-4 text-gray-700">
                MPU6050是一款常用的6轴运动处理传感器，包含3轴加速度计和3轴陀螺仪。与MPU6050通信通常使用I2C协议，下面演示如何从其寄存器中读取数据。
            </p>
            <div class="flex flex-wrap gap-4">
                <div class="flex-1 min-w-[300px] bg-blue-50 p-4 rounded border border-blue-100">
                    <h3 class="font-medium text-primary mb-2"><i class="fa fa-list-ol mr-2"></i>读取步骤</h3>
                    <ol class="list-decimal list-inside space-y-1 text-gray-700">
                        <li>初始化I2C通信</li>
                        <li>发送MPU6050的设备地址和读写位</li>
                        <li>发送要读取的寄存器地址</li>
                        <li>重新启动通信并发送读命令</li>
                        <li>接收从寄存器返回的数据</li>
                    </ol>
                </div>
                <div class="flex-1 min-w-[300px] bg-green-50 p-4 rounded border border-green-100">
                    <h3 class="font-medium text-secondary mb-2"><i class="fa fa-play-circle mr-2"></i>操作指南</h3>
                    <ul class="list-disc list-inside space-y-1 text-gray-700">
                        <li>点击"开始演示"按钮观看完整流程</li>
                        <li>点击单个寄存器可单独读取该寄存器</li>
                        <li>寄存器数据会实时更新到右侧数据区</li>
                        <li>点击"重置"可重新开始演示</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- 演示区域 -->
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            <!-- MPU6050 模型 -->
            <section class="lg:col-span-2">
                <div class="bg-white rounded-lg shadow-md p-6">
                    <h2 class="text-xl font-semibold mb-6 text-dark flex items-center">
                        <i class="fa fa-microchip text-accent mr-2"></i>MPU6050 寄存器模型
                    </h2>
                    
                    <div class="flex justify-center mb-8">
                        <div class="relative w-full max-w-2xl">
                            <!-- MPU6050 芯片外观 -->
                            <div class="border-4 border-dark rounded-lg bg-gray-100 p-6 relative">
                                <div class="absolute top-2 left-2 text-xs bg-dark text-white px-2 py-1 rounded">MPU6050</div>
                                
                                <!-- 左侧引脚 -->
                                <div class="absolute left-0 top-1/2 transform -translate-y-1/2 -translate-x-4 flex flex-col gap-4">
                                    <div class="pin" title="VCC"></div>
                                    <div class="pin" title="GND"></div>
                                    <div class="pin" title="SCL" id="sclPin"></div>
                                    <div class="pin" title="SDA" id="sdaPin"></div>
                                    <div class="pin" title="XDA"></div>
                                    <div class="pin" title="XCL"></div>
                                </div>
                                
                                <!-- 右侧引脚 -->
                                <div class="absolute right-0 top-1/2 transform -translate-y-1/2 translate-x-4 flex flex-col gap-4">
                                    <div class="pin" title="AD0"></div>
                                    <div class="pin" title="INT"></div>
                                    <div class="pin" title="FSYNC"></div>
                                    <div class="pin" title="AUX_CL"></div>
                                    <div class="pin" title="AUX_DA"></div>
                                </div>
                                
                                <!-- 寄存器区域 -->
                                <div class="register-grid">
                                    <!-- 配置寄存器 -->
                                    <div class="register bg-gray-200 p-2 rounded text-center cursor-pointer hover:bg-gray-300 transition" data-reg="PWR_MGMT_1" data-addr="0x6B">
                                        <div class="text-xs font-medium text-gray-700">PWR_MGMT_1</div>
                                        <div class="text-xs text-gray-500">0x6B</div>
                                    </div>
                                    <div class="register bg-gray-200 p-2 rounded text-center cursor-pointer hover:bg-gray-300 transition" data-reg="SMPLRT_DIV" data-addr="0x19">
                                        <div class="text-xs font-medium text-gray-700">SMPLRT_DIV</div>
                                        <div class="text-xs text-gray-500">0x19</div>
                                    </div>
                                    <div class="register bg-gray-200 p-2 rounded text-center cursor-pointer hover:bg-gray-300 transition" data-reg="CONFIG" data-addr="0x1A">
                                        <div class="text-xs font-medium text-gray-700">CONFIG</div>
                                        <div class="text-xs text-gray-500">0x1A</div>
                                    </div>
                                    <div class="register bg-gray-200 p-2 rounded text-center cursor-pointer hover:bg-gray-300 transition" data-reg="GYRO_CONFIG" data-addr="0x1B">
                                        <div class="text-xs font-medium text-gray-700">GYRO_CONFIG</div>
                                        <div class="text-xs text-gray-500">0x1B</div>
                                    </div>
                                    <div class="register bg-gray-200 p-2 rounded text-center cursor-pointer hover:bg-gray-300 transition" data-reg="ACCEL_CONFIG" data-addr="0x1C">
                                        <div class="text-xs font-medium text-gray-700">ACCEL_CONFIG</div>
                                        <div class="text-xs text-gray-500">0x1C</div>
                                    </div>
                                    
                                    <!-- 加速度计数据寄存器 -->
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_XOUT_H" data-addr="0x3B">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_XOUT_H</div>
                                        <div class="text-xs text-blue-600">0x3B</div>
                                    </div>
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_XOUT_L" data-addr="0x3C">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_XOUT_L</div>
                                        <div class="text-xs text-blue-600">0x3C</div>
                                    </div>
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_YOUT_H" data-addr="0x3D">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_YOUT_H</div>
                                        <div class="text-xs text-blue-600">0x3D</div>
                                    </div>
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_YOUT_L" data-addr="0x3E">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_YOUT_L</div>
                                        <div class="text-xs text-blue-600">0x3E</div>
                                    </div>
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_ZOUT_H" data-addr="0x3F">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_ZOUT_H</div>
                                        <div class="text-xs text-blue-600">0x3F</div>
                                    </div>
                                    <div class="register bg-blue-100 p-2 rounded text-center cursor-pointer hover:bg-blue-200 transition" data-reg="ACCEL_ZOUT_L" data-addr="0x40">
                                        <div class="text-xs font-medium text-blue-800">ACCEL_ZOUT_L</div>
                                        <div class="text-xs text-blue-600">0x40</div>
                                    </div>
                                    
                                    <!-- 陀螺仪数据寄存器 -->
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_XOUT_H" data-addr="0x43">
                                        <div class="text-xs font-medium text-green-800">GYRO_XOUT_H</div>
                                        <div class="text-xs text-green-600">0x43</div>
                                    </div>
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_XOUT_L" data-addr="0x44">
                                        <div class="text-xs font-medium text-green-800">GYRO_XOUT_L</div>
                                        <div class="text-xs text-green-600">0x44</div>
                                    </div>
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_YOUT_H" data-addr="0x45">
                                        <div class="text-xs font-medium text-green-800">GYRO_YOUT_H</div>
                                        <div class="text-xs text-green-600">0x45</div>
                                    </div>
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_YOUT_L" data-addr="0x46">
                                        <div class="text-xs font-medium text-green-800">GYRO_YOUT_L</div>
                                        <div class="text-xs text-green-600">0x46</div>
                                    </div>
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_ZOUT_H" data-addr="0x47">
                                        <div class="text-xs font-medium text-green-800">GYRO_ZOUT_H</div>
                                        <div class="text-xs text-green-600">0x47</div>
                                    </div>
                                    <div class="register bg-green-100 p-2 rounded text-center cursor-pointer hover:bg-green-200 transition" data-reg="GYRO_ZOUT_L" data-addr="0x48">
                                        <div class="text-xs font-medium text-green-800">GYRO_ZOUT_L</div>
                                        <div class="text-xs text-green-600">0x48</div>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- 数据流指示器 -->
                            <div id="dataFlowIndicator" class="hidden absolute top-1/2 left-full ml-4 transform -translate-y-1/2">
                                <div class="w-3 h-3 bg-primary rounded-full"></div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="text-center">
                        <button id="startDemoBtn" class="bg-accent hover:bg-amber-600 text-white px-6 py-2 rounded-lg shadow-md transition flex items-center mx-auto">
                            <i class="fa fa-play mr-2"></i>开始演示
                        </button>
                    </div>
                </div>
            </section>
            
            <!-- 数据显示区域 -->
            <section>
                <div class="bg-white rounded-lg shadow-md p-6 h-full">
                    <h2 class="text-xl font-semibold mb-6 text-dark flex items-center">
                        <i class="fa fa-bar-chart text-primary mr-2"></i>读取的数据
                    </h2>
                    
                    <!-- 通信日志 -->
                    <div class="mb-6">
                        <h3 class="font-medium text-gray-700 mb-2 border-b pb-1">通信日志</h3>
                        <div id="logArea" class="bg-gray-50 h-40 overflow-y-auto p-3 rounded border text-sm text-gray-700">
                            <div class="text-gray-500">等待开始通信...</div>
                        </div>
                    </div>
                    
                    <!-- 寄存器数据 -->
                    <div>
                        <h3 class="font-medium text-gray-700 mb-2 border-b pb-1">寄存器数据</h3>
                        <div id="dataDisplay" class="space-y-3">
                            <div class="grid grid-cols-2 gap-2">
                                <div class="text-sm text-gray-600">寄存器:</div>
                                <div id="selectedReg" class="text-sm font-medium">-</div>
                            </div>
                            <div class="grid grid-cols-2 gap-2">
                                <div class="text-sm text-gray-600">地址:</div>
                                <div id="regAddress" class="text-sm font-medium">-</div>
                            </div>
                            <div class="grid grid-cols-2 gap-2">
                                <div class="text-sm text-gray-600">值:</div>
                                <div id="regValue" class="text-sm font-medium">-</div>
                            </div>
                            <div class="grid grid-cols-2 gap-2">
                                <div class="text-sm text-gray-600">十进制:</div>
                                <div id="regDecimal" class="text-sm font-medium">-</div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 传感器数据 -->
                    <div class="mt-6">
                        <h3 class="font-medium text-gray-700 mb-2 border-b pb-1">传感器数据</h3>
                        <div class="space-y-2">
                            <div class="flex justify-between items-center p-2 bg-blue-50 rounded">
                                <span class="text-sm text-blue-800">加速度 X:</span>
                                <span id="accelX" class="text-sm font-medium">0.00 g</span>
                            </div>
                            <div class="flex justify-between items-center p-2 bg-blue-50 rounded">
                                <span class="text-sm text-blue-800">加速度 Y:</span>
                                <span id="accelY" class="text-sm font-medium">0.00 g</span>
                            </div>
                            <div class="flex justify-between items-center p-2 bg-blue-50 rounded">
                                <span class="text-sm text-blue-800">加速度 Z:</span>
                                <span id="accelZ" class="text-sm font-medium">0.00 g</span>
                            </div>
                            <div class="flex justify-between items-center p-2 bg-green-50 rounded">
                                <span class="text-sm text-green-800">角速度 X:</span>
                                <span id="gyroX" class="text-sm font-medium">0.00 °/s</span>
                            </div>
                            <div class="flex justify-between items-center p-2 bg-green-50 rounded">
                                <span class="text-sm text-green-800">角速度 Y:</span>
                                <span id="gyroY" class="text-sm font-medium">0.00 °/s</span>
                            </div>
                            <div class="flex justify-between items-center p-2 bg-green-50 rounded">
                                <span class="text-sm text-green-800">角速度 Z:</span>
                                <span id="gyroZ" class="text-sm font-medium">0.00 °/s</span>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </main>

    <!-- 帮助模态框 -->
    <div id="infoModal" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 hidden">
        <div class="bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto">
            <div class="p-6 border-b">
                <div class="flex justify-between items-center">
                    <h2 class="text-xl font-bold text-dark">MPU6050 寄存器读取说明</h2>
                    <button id="closeInfoBtn" class="text-gray-500 hover:text-gray-700">
                        <i class="fa fa-times text-xl"></i>
                    </button>
                </div>
            </div>
            <div class="p-6">
                <h3 class="font-semibold text-lg mb-3 text-primary">MPU6050 简介</h3>
                <p class="mb-4 text-gray-700">
                    MPU6050是一款集成了3轴加速度计和3轴陀螺仪的6轴运动处理传感器。它通过I2C接口与微控制器通信，广泛应用于无人机、机器人、平衡车等设备中。
                </p>
                
                <h3 class="font-semibold text-lg mb-3 text-primary">寄存器读取原理</h3>
                <p class="mb-3 text-gray-700">
                    MPU6050内部包含多个寄存器，每个寄存器都有一个特定的地址。要读取传感器数据，需要：
                </p>
                <ol class="list-decimal list-inside mb-4 pl-4 space-y-2 text-gray-700">
                    <li>通过I2C总线发送MPU6050的设备地址(通常为0x68或0x69)</li>
                    <li>发送要读取的寄存器地址</li>
                    <li>读取该寄存器中的数据</li>
                </ol>
                
                <h3 class="font-semibold text-lg mb-3 text-primary">数据格式</h3>
                <p class="mb-3 text-gray-700">
                    加速度计和陀螺仪的数据分别存储在两个8位寄存器中(高8位和低8位)，需要将这两个值组合成一个16位有符号整数，然后根据传感器配置进行转换。
                </p>
                
                <h3 class="font-semibold text-lg mb-3 text-primary">常用寄存器</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
                    <div class="bg-gray-50 p-3 rounded">
                        <h4 class="font-medium text-gray-800 mb-2">配置寄存器</h4>
                        <ul class="text-sm text-gray-700 space-y-1">
                            <li><span class="font-medium">0x6B (PWR_MGMT_1):</span> 电源管理</li>
                            <li><span class="font-medium">0x19 (SMPLRT_DIV):</span> 采样率分频</li>
                            <li><span class="font-medium">0x1B (GYRO_CONFIG):</span> 陀螺仪配置</li>
                            <li><span class="font-medium">0x1C (ACCEL_CONFIG):</span> 加速度计配置</li>
                        </ul>
                    </div>
                    <div class="bg-gray-50 p-3 rounded">
                        <h4 class="font-medium text-gray-800 mb-2">数据寄存器</h4>
                        <ul class="text-sm text-gray-700 space-y-1">
                            <li><span class="font-medium">0x3B-0x40:</span> 加速度计数据</li>
                            <li><span class="font-medium">0x43-0x48:</span> 陀螺仪数据</li>
                            <li><span class="font-medium">每个轴:</span> 高8位 + 低8位</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 页面元素
        const startDemoBtn = document.getElementById('startDemoBtn');
        const resetBtn = document.getElementById('resetBtn');
        const infoBtn = document.getElementById('infoBtn');
        const closeInfoBtn = document.getElementById('closeInfoBtn');
        const infoModal = document.getElementById('infoModal');
        const logArea = document.getElementById('logArea');
        const dataFlowIndicator = document.getElementById('dataFlowIndicator');
        const sclPin = document.getElementById('sclPin');
        const sdaPin = document.getElementById('sdaPin');
        
        // 数据显示元素
        const selectedReg = document.getElementById('selectedReg');
        const regAddress = document.getElementById('regAddress');
        const regValue = document.getElementById('regValue');
        const regDecimal = document.getElementById('regDecimal');
        
        // 传感器数据显示
        const accelX = document.getElementById('accelX');
        const accelY = document.getElementById('accelY');
        const accelZ = document.getElementById('accelZ');
        const gyroX = document.getElementById('gyroX');
        const gyroY = document.getElementById('gyroY');
        const gyroZ = document.getElementById('gyroZ');
        
        // 寄存器列表
        const registers = document.querySelectorAll('.register');
        
        // 模拟数据生成函数
        function generateRegisterValue(regName) {
            // 为不同类型的寄存器生成合理的模拟值
            if (regName.includes('ACCEL')) {
                // 加速度计数据 (±2g 量程时约为 ±16384)
                return Math.floor(Math.random() * 32768) - 16384;
            } else if (regName.includes('GYRO')) {
                // 陀螺仪数据 (±250°/s 量程时约为 ±131)
                return Math.floor(Math.random() * 262) - 131;
            } else {
                // 配置寄存器 (0-255)
                return Math.floor(Math.random() * 256);
            }
        }
        
        // 格式化十六进制值
        function formatHex(value) {
            return `0x${value.toString(16).toUpperCase().padStart(2, '0')}`;
        }
        
        // 记录日志
        function log(message) {
            const logEntry = document.createElement('div');
            logEntry.className = 'mb-1';
            logEntry.innerHTML = `<span class="text-xs text-gray-500">${new Date().toLocaleTimeString()}</span> ${message}`;
            logArea.appendChild(logEntry);
            logArea.scrollTop = logArea.scrollHeight;
        }
        
        // 模拟I2C通信
        function simulateI2CCommunication(regElement) {
            const regName = regElement.dataset.reg;
            const regAddr = regElement.dataset.addr;
            
            // 重置所有寄存器状态
            registers.forEach(reg => reg.classList.remove('register-active', 'bg-primary', 'text-white'));
            
            // 清除之前的数据
            log(`开始读取寄存器 ${regName} (${regAddr})`);
            
            return new Promise(resolve => {
                // 步骤1: 发送设备地址
                setTimeout(() => {
                    log(`发送设备地址: 0x68 (MPU6050地址) + 写位`);
                    sclPin.classList.add('bg-primary');
                    sdaPin.classList.add('bg-primary');
                    
                    // 步骤2: 发送寄存器地址
                    setTimeout(() => {
                        log(`发送寄存器地址: ${regAddr}`);
                        
                        // 步骤3: 发送读命令
                        setTimeout(() => {
                            log(`发送读命令`);
                            
                            // 步骤4: 读取数据
                            setTimeout(() => {
                                // 生成寄存器值
                                const value = generateRegisterValue(regName);
                                const hexValue = formatHex(value & 0xFF); // 取低8位
                                const decimalValue = value & 0xFF;
                                
                                // 更新UI
                                regElement.classList.add('register-active', 'bg-primary', 'text-white');
                                selectedReg.textContent = regName;
                                regAddress.textContent = regAddr;
                                regValue.textContent = hexValue;
                                regDecimal.textContent = decimalValue;
                                
                                log(`成功读取数据: ${hexValue} (十进制: ${decimalValue})`);
                                
                                // 显示数据流动
                                dataFlowIndicator.classList.remove('hidden');
                                dataFlowIndicator.classList.add('data-flow');
                                
                                // 处理加速度计和陀螺仪数据
                                updateSensorData(regName, value);
                                
                                // 重置引脚状态
                                setTimeout(() => {
                                    sclPin.classList.remove('bg-primary');
                                    sdaPin.classList.remove('bg-primary');
                                    dataFlowIndicator.classList.add('hidden');
                                    dataFlowIndicator.classList.remove('data-flow');
                                    resolve();
                                }, 1000);
                            }, 800);
                        }, 800);
                    }, 800);
                }, 500);
            });
        }
        
        // 更新传感器数据显示
        function updateSensorData(regName, value) {
            // 存储高8位数据，等待低8位数据到来
            if (!window.sensorData) window.sensorData = {};
            
            if (regName === 'ACCEL_XOUT_H') {
                window.sensorData.accelXH = value;
            } else if (regName === 'ACCEL_XOUT_L') {
                if (window.sensorData.accelXH !== undefined) {
                    // 组合高低8位
                    const accelXValue = (window.sensorData.accelXH << 8) | (value & 0xFF);
                    // 转换为g (假设量程为±2g，灵敏度16384 LSB/g)
                    const accelXG = (accelXValue / 16384).toFixed(2);
                    accelX.textContent = `${accelXG} g`;
                    delete window.sensorData.accelXH;
                }
            } else if (regName === 'ACCEL_YOUT_H') {
                window.sensorData.accelYH = value;
            } else if (regName === 'ACCEL_YOUT_L') {
                if (window.sensorData.accelYH !== undefined) {
                    const accelYValue = (window.sensorData.accelYH << 8) | (value & 0xFF);
                    const accelYG = (accelYValue / 16384).toFixed(2);
                    accelY.textContent = `${accelYG} g`;
                    delete window.sensorData.accelYH;
                }
            } else if (regName === 'ACCEL_ZOUT_H') {
                window.sensorData.accelZH = value;
            } else if (regName === 'ACCEL_ZOUT_L') {
                if (window.sensorData.accelZH !== undefined) {
                    const accelZValue = (window.sensorData.accelZH << 8) | (value & 0xFF);
                    const accelZG = (accelZValue / 16384).toFixed(2);
                    accelZ.textContent = `${accelZG} g`;
                    delete window.sensorData.accelZH;
                }
            } else if (regName === 'GYRO_XOUT_H') {
                window.sensorData.gyroXH = value;
            } else if (regName === 'GYRO_XOUT_L') {
                if (window.sensorData.gyroXH !== undefined) {
                    const gyroXValue = (window.sensorData.gyroXH << 8) | (value & 0xFF);
                    // 转换为°/s (假设量程为±250°/s，灵敏度131 LSB/(°/s))
                    const gyroXDeg = (gyroXValue / 131).toFixed(2);
                    gyroX.textContent = `${gyroXDeg} °/s`;
                    delete window.sensorData.gyroXH;
                }
            } else if (regName === 'GYRO_YOUT_H') {
                window.sensorData.gyroYH = value;
            } else if (regName === 'GYRO_YOUT_L') {
                if (window.sensorData.gyroYH !== undefined) {
                    const gyroYValue = (window.sensorData.gyroYH << 8) | (value & 0xFF);
                    const gyroYDeg = (gyroYValue / 131).toFixed(2);
                    gyroY.textContent = `${gyroYDeg} °/s`;
                    delete window.sensorData.gyroYH;
                }
            } else if (regName === 'GYRO_ZOUT_H') {
                window.sensorData.gyroZH = value;
            } else if (regName === 'GYRO_ZOUT_L') {
                if (window.sensorData.gyroZH !== undefined) {
                    const gyroZValue = (window.sensorData.gyroZH << 8) | (value & 0xFF);
                    const gyroZDeg = (gyroZValue / 131).toFixed(2);
                    gyroZ.textContent = `${gyroZDeg} °/s`;
                    delete window.sensorData.gyroZH;
                }
            }
        }
        
        // 完整演示流程
        async function runFullDemo() {
            // 禁用按钮防止重复点击
            startDemoBtn.disabled = true;
            startDemoBtn.classList.add('opacity-70', 'cursor-not-allowed');
            log('开始完整演示流程...');
            
            // 先读取配置寄存器
            const configRegs = [
                document.querySelector('[data-reg="PWR_MGMT_1"]'),
                document.querySelector('[data-reg="SMPLRT_DIV"]'),
                document.querySelector('[data-reg="GYRO_CONFIG"]'),
                document.querySelector('[data-reg="ACCEL_CONFIG"]')
            ];
            
            for (const reg of configRegs) {
                await simulateI2CCommunication(reg);
                await new Promise(resolve => setTimeout(resolve, 500));
            }
            
            // 然后读取加速度计数据
            const accelRegs = [
                document.querySelector('[data-reg="ACCEL_XOUT_H"]'),
                document.querySelector('[data-reg="ACCEL_XOUT_L"]'),
                document.querySelector('[data-reg="ACCEL_YOUT_H"]'),
                document.querySelector('[data-reg="ACCEL_YOUT_L"]'),
                document.querySelector('[data-reg="ACCEL_ZOUT_H"]'),
                document.querySelector('[data-reg="ACCEL_ZOUT_L"]')
            ];
            
            for (const reg of accelRegs) {
                await simulateI2CCommunication(reg);
                await new Promise(resolve => setTimeout(resolve, 500));
            }
            
            // 最后读取陀螺仪数据
            const gyroRegs = [
                document.querySelector('[data-reg="GYRO_XOUT_H"]'),
                document.querySelector('[data-reg="GYRO_XOUT_L"]'),
                document.querySelector('[data-reg="GYRO_YOUT_H"]'),
                document.querySelector('[data-reg="GYRO_YOUT_L"]'),
                document.querySelector('[data-reg="GYRO_ZOUT_H"]'),
                document.querySelector('[data-reg="GYRO_ZOUT_L"]')
            ];
            
            for (const reg of gyroRegs) {
                await simulateI2CCommunication(reg);
                await new Promise(resolve => setTimeout(resolve, 500));
            }
            
            log('演示流程完成!');
            startDemoBtn.disabled = false;
            startDemoBtn.classList.remove('opacity-70', 'cursor-not-allowed');
        }
        
        // 重置演示
        function resetDemo() {
            registers.forEach(reg => reg.classList.remove('register-active', 'bg-primary', 'text-white'));
            logArea.innerHTML = '<div class="text-gray-500">等待开始通信...</div>';
            selectedReg.textContent = '-';
            regAddress.textContent = '-';
            regValue.textContent = '-';
            regDecimal.textContent = '-';
            
            accelX.textContent = '0.00 g';
            accelY.textContent = '0.00 g';
            accelZ.textContent = '0.00 g';
            gyroX.textContent = '0.00 °/s';
            gyroY.textContent = '0.00 °/s';
            gyroZ.textContent = '0.00 °/s';
            
            sclPin.classList.remove('bg-primary');
            sdaPin.classList.remove('bg-primary');
            dataFlowIndicator.classList.add('hidden');
            dataFlowIndicator.classList.remove('data-flow');
            
            window.sensorData = {};
            
            startDemoBtn.disabled = false;
            startDemoBtn.classList.remove('opacity-70', 'cursor-not-allowed');
        }
        
        // 事件监听
        startDemoBtn.addEventListener('click', runFullDemo);
        resetBtn.addEventListener('click', resetDemo);
        
        // 寄存器点击事件
        registers.forEach(reg => {
            reg.addEventListener('click', async () => {
                startDemoBtn.disabled = true;
                startDemoBtn.classList.add('opacity-70', 'cursor-not-allowed');
                
                await simulateI2CCommunication(reg);
                
                startDemoBtn.disabled = false;
                startDemoBtn.classList.remove('opacity-70', 'cursor-not-allowed');
            });
        });
        
        // 帮助模态框
        infoBtn.addEventListener('click', () => {
            infoModal.classList.remove('hidden');
        });
        
        closeInfoBtn.addEventListener('click', () => {
            infoModal.classList.add('hidden');
        });
        
        // 点击模态框外部关闭
        infoModal.addEventListener('click', (e) => {
            if (e.target === infoModal) {
                infoModal.classList.add('hidden');
            }
        });
    </script>
</body>
</html>
